package StringSeries;

public class ReverseString_2_Solution_541 {
    public String reverseStr(String s, int k) {
        // 做法：for循环每次i + 2k
        char[] sArray = s.toCharArray();

        for (int i = 0; i < sArray.length; i = i + 2 * k) {
            int left = i;
            // 巧妙之处，直接处理了两种情况
            int right = Math.min(left + k - 1, sArray.length - 1);
            while (left < right) {
                // 反转前k个字符
                char temp = sArray[right];
                sArray[right] = sArray[left];
                sArray[left] = temp;
                left++;
                right--;
            }
        }

        String str = new String(sArray);
        return str;
    }
}
